const KoaRouter = require('koa-router')
const router = new KoaRouter();
const axios = require('axios').default;
const JWT = require('jsonwebtoken')
const key = '1231231'
router.post('/login', async ctx => {
  // 获取发送过来的 code
  const { code } = ctx.request.body
  // wx 小程序参数
  const wx = {
    appid: 'wx2bae7e5b372f752d',
    secret: 'd88b847627b47fc64ee6230186945a98',
    js_code: code,
    grant_type: 'authorization_code'
  }

  // 发送请求  请求微信开放接口 换取 openid
  const resp = await axios.get('https://api.weixin.qq.com/sns/jscode2session', { params: wx });
  // 返回小程序前端结果
  ctx.body = {
    code: 200,
    message: 'ok',
    data: {
      token: JWT.sign({
        ...resp.data
      }, key),
      userInfo: {
        
      }
    }
  }
})


module.exports = router